<?php
/**
 * 简单判断浏览器类型
 * @authors yutent (yutent@doui.cc)
 * @date    2015-07-22 10:58:25
 *
 */

class Do_Browser {

	protected $ua = NULL;
	protected $phone = [
		'iPhone' => '\biphone\b|\bipod\b',
		'BlackBerry' => 'blackberry|\bbb10\b|rim[0-9]+',
        'HTC' => 'htc one x|htc.*(one|sensation|evo|vision|explorer|6800|8100|8900|a7272|s510e|c110e|legend|desire|t8282)|apx515ckt|qtek9090|apa9292kt|hd_mini|sensation.*z710e|pg86100|z715e|desire.*(a8181|hd)|adr6200|adr6400l|adr6425|001ht|inspire 4g|android.*\bevo\b|t-mobile g1|z520m',
        'Nexus' => 'nexus one|nexus s|nexus 4|nexus 5|nexus 6|galaxy.*nexus|nexus.*mobile',
        'Motorola' => 'motorola|droidx|droid bionic|milestone|(\bdroid\b.*)build|xoom|hri39|mot-|a1260|a1680|a555|a853|a855|a953|a955|a956|motorola.*electrify|motorola.*i1|i867|i940|mb200|mb300|mb501|mb502|mb508|mb511|mb520|mb525|mb526|mb611|mb612|mb632|mb810|mb855|mb860|mb861|mb865|mb870|me501|me502|me511|me525|me600|me632|me722|me811|me860|me863|me865|mt620|mt710|mt716|mt720|mt810|mt870|mt917|motorola.*titanium|wx435|wx445|xt300|xt301|xt311|xt316|xt317|xt319|xt320|xt390|xt502|xt530|xt531|xt532|xt535|xt603|xt610|xt611|xt615|xt681|xt701|xt702|xt711|xt720|xt800|xt806|xt860|xt862|xt875|xt882|xt883|xt894|xt901|xt907|xt909|xt910|xt912|xt928|xt926|xt915|xt919|xt925|xt1021|\bmoto e\b',
        'Samsung' => '(samsung)?.*(sm-g9250|gt-19300|sgh-i337|bgt-s5230|gt-b2100|gt-b2700|gt-b2710|gt-b3210|gt-b3310|gt-b3410|gt-b3730|gt-b3740|gt-b5510|gt-b5512|gt-b5722|gt-b6520|gt-b7300|gt-b7320|gt-b7330|gt-b7350|gt-b7510|gt-b7722|gt-b7800|gt-c3010|gt-c3011|gt-c3060|gt-c3200|gt-c3212|gt-c3212i|gt-c3262|gt-c3222|gt-c3300|gt-c3300k|gt-c3303|gt-c3303k|gt-c3310|gt-c3322|gt-c3330|gt-c3350|gt-c3500|gt-c3510|gt-c3530|gt-c3630|gt-c3780|gt-c5010|gt-c5212|gt-c6620|gt-c6625|gt-c6712|gt-e1050|gt-e1070|gt-e1075|gt-e1080|gt-e1081|gt-e1085|gt-e1087|gt-e1100|gt-e1107|gt-e1110|gt-e1120|gt-e1125|gt-e1130|gt-e1160|gt-e1170|gt-e1175|gt-e1180|gt-e1182|gt-e1200|gt-e1210|gt-e1225|gt-e1230|gt-e1390|gt-e2100|gt-e2120|gt-e2121|gt-e2152|gt-e2220|gt-e2222|gt-e2230|gt-e2232|gt-e2250|gt-e2370|gt-e2550|gt-e2652|gt-e3210|gt-e3213|gt-i5500|gt-i5503|gt-i5700|gt-i5800|gt-i5801|gt-i6410|gt-i6420|gt-i7110|gt-i7410|gt-i7500|gt-i8000|gt-i8150|gt-i8160|gt-i8190|gt-i8320|gt-i8330|gt-i8350|gt-i8530|gt-i8700|gt-i8703|gt-i8910|gt-i9000|gt-i9001|gt-i9003|gt-i9010|gt-i9020|gt-i9023|gt-i9070|gt-i9082|gt-i9100|gt-i9103|gt-i9220|gt-i9250|gt-i9300|gt-i9305|gt-i9500|gt-i9505|gt-m3510|gt-m5650|gt-m7500|gt-m7600|gt-m7603|gt-m8800|gt-m8910|gt-n7000|gt-s3110|gt-s3310|gt-s3350|gt-s3353|gt-s3370|gt-s3650|gt-s3653|gt-s3770|gt-s3850|gt-s5210|gt-s5220|gt-s5229|gt-s5230|gt-s5233|gt-s5250|gt-s5253|gt-s5260|gt-s5263|gt-s5270|gt-s5300|gt-s5330|gt-s5350|gt-s5360|gt-s5363|gt-s5369|gt-s5380|gt-s5380d|gt-s5560|gt-s5570|gt-s5600|gt-s5603|gt-s5610|gt-s5620|gt-s5660|gt-s5670|gt-s5690|gt-s5750|gt-s5780|gt-s5830|gt-s5839|gt-s6102|gt-s6500|gt-s7070|gt-s7200|gt-s7220|gt-s7230|gt-s7233|gt-s7250|gt-s7500|gt-s7530|gt-s7550|gt-s7562|gt-s7710|gt-s8000|gt-s8003|gt-s8500|gt-s8530|gt-s8600|sch-a310|sch-a530|sch-a570|sch-a610|sch-a630|sch-a650|sch-a790|sch-a795|sch-a850|sch-a870|sch-a890|sch-a930|sch-a950|sch-a970|sch-a990|sch-i100|sch-i110|sch-i400|sch-i405|sch-i500|sch-i510|sch-i515|sch-i600|sch-i730|sch-i760|sch-i770|sch-i830|sch-i910|sch-i920|sch-i959|sch-lc11|sch-n150|sch-n300|sch-r100|sch-r300|sch-r351|sch-r400|sch-r410|sch-t300|sch-u310|sch-u320|sch-u350|sch-u360|sch-u365|sch-u370|sch-u380|sch-u410|sch-u430|sch-u450|sch-u460|sch-u470|sch-u490|sch-u540|sch-u550|sch-u620|sch-u640|sch-u650|sch-u660|sch-u700|sch-u740|sch-u750|sch-u810|sch-u820|sch-u900|sch-u940|sch-u960|scs-26uc|sgh-a107|sgh-a117|sgh-a127|sgh-a137|sgh-a157|sgh-a167|sgh-a177|sgh-a187|sgh-a197|sgh-a227|sgh-a237|sgh-a257|sgh-a437|sgh-a517|sgh-a597|sgh-a637|sgh-a657|sgh-a667|sgh-a687|sgh-a697|sgh-a707|sgh-a717|sgh-a727|sgh-a737|sgh-a747|sgh-a767|sgh-a777|sgh-a797|sgh-a817|sgh-a827|sgh-a837|sgh-a847|sgh-a867|sgh-a877|sgh-a887|sgh-a897|sgh-a927|sgh-b100|sgh-b130|sgh-b200|sgh-b220|sgh-c100|sgh-c110|sgh-c120|sgh-c130|sgh-c140|sgh-c160|sgh-c170|sgh-c180|sgh-c200|sgh-c207|sgh-c210|sgh-c225|sgh-c230|sgh-c417|sgh-c450|sgh-d307|sgh-d347|sgh-d357|sgh-d407|sgh-d415|sgh-d780|sgh-d807|sgh-d980|sgh-e105|sgh-e200|sgh-e315|sgh-e316|sgh-e317|sgh-e335|sgh-e590|sgh-e635|sgh-e715|sgh-e890|sgh-f300|sgh-f480|sgh-i200|sgh-i300|sgh-i320|sgh-i550|sgh-i577|sgh-i600|sgh-i607|sgh-i617|sgh-i627|sgh-i637|sgh-i677|sgh-i700|sgh-i717|sgh-i727|sgh-i747m|sgh-i777|sgh-i780|sgh-i827|sgh-i847|sgh-i857|sgh-i896|sgh-i897|sgh-i900|sgh-i907|sgh-i917|sgh-i927|sgh-i937|sgh-i997|sgh-j150|sgh-j200|sgh-l170|sgh-l700|sgh-m110|sgh-m150|sgh-m200|sgh-n105|sgh-n500|sgh-n600|sgh-n620|sgh-n625|sgh-n700|sgh-n710|sgh-p107|sgh-p207|sgh-p300|sgh-p310|sgh-p520|sgh-p735|sgh-p777|sgh-q105|sgh-r210|sgh-r220|sgh-r225|sgh-s105|sgh-s307|sgh-t109|sgh-t119|sgh-t139|sgh-t209|sgh-t219|sgh-t229|sgh-t239|sgh-t249|sgh-t259|sgh-t309|sgh-t319|sgh-t329|sgh-t339|sgh-t349|sgh-t359|sgh-t369|sgh-t379|sgh-t409|sgh-t429|sgh-t439|sgh-t459|sgh-t469|sgh-t479|sgh-t499|sgh-t509|sgh-t519|sgh-t539|sgh-t559|sgh-t589|sgh-t609|sgh-t619|sgh-t629|sgh-t639|sgh-t659|sgh-t669|sgh-t679|sgh-t709|sgh-t719|sgh-t729|sgh-t739|sgh-t746|sgh-t749|sgh-t759|sgh-t769|sgh-t809|sgh-t819|sgh-t839|sgh-t919|sgh-t929|sgh-t939|sgh-t959|sgh-t989|sgh-u100|sgh-u200|sgh-u800|sgh-v205|sgh-v206|sgh-x100|sgh-x105|sgh-x120|sgh-x140|sgh-x426|sgh-x427|sgh-x475|sgh-x495|sgh-x497|sgh-x507|sgh-x600|sgh-x610|sgh-x620|sgh-x630|sgh-x700|sgh-x820|sgh-x890|sgh-z130|sgh-z150|sgh-z170|sgh-zx10|sgh-zx20|shw-m110|sph-a120|sph-a400|sph-a420|sph-a460|sph-a500|sph-a560|sph-a600|sph-a620|sph-a660|sph-a700|sph-a740|sph-a760|sph-a790|sph-a800|sph-a820|sph-a840|sph-a880|sph-a900|sph-a940|sph-a960|sph-d600|sph-d700|sph-d710|sph-d720|sph-i300|sph-i325|sph-i330|sph-i350|sph-i500|sph-i600|sph-i700|sph-l700|sph-m100|sph-m220|sph-m240|sph-m300|sph-m305|sph-m320|sph-m330|sph-m350|sph-m360|sph-m370|sph-m380|sph-m510|sph-m540|sph-m550|sph-m560|sph-m570|sph-m580|sph-m610|sph-m620|sph-m630|sph-m800|sph-m810|sph-m850|sph-m900|sph-m910|sph-m920|sph-m930|sph-n100|sph-n200|sph-n240|sph-n300|sph-n400|sph-z400|swc-e100|sch-i909|gt-n7100|gt-n7105|sch-i535|sm-n900a|sgh-i317|sgh-t999l|gt-s5360b|gt-i8262|gt-s6802|gt-s6312|gt-s6310|gt-s5312|gt-s5310|gt-i9105|gt-i8510|gt-s6790n|sm-g7105|sm-n9005|gt-s5301|gt-i9295|gt-i9195|sm-c101|gt-s7392|gt-s7560|gt-b7610|gt-i5510|gt-s7582|gt-s7530e|gt-i8750|sm-g9006v|sm-g9008v|sm-g9009d|sm-g900a|sm-g900d|sm-g900f|sm-g900h|sm-g900i|sm-g900j|sm-g900k|sm-g900l|sm-g900m|sm-g900p|sm-g900r4|sm-g900s|sm-g900t|sm-g900v|sm-g900w8|sm-n900t|shv-e160k|sch-p709|sch-p729|sm-t2558|gt-i9205)|samsung',
        'LG' => '\blg\b;|lg[- ]?(c800|c900|e400|e610|e900|e975|e-900|f160|f180k|f180l|f180s|730|855|l160|ls740|ls840|ls970|lu6200|ms690|ms695|ms770|ms840|ms870|ms910|p500|p700|p705|vm696|as680|as695|ax840|c729|e970|gs505|272|c395|e739bk|e960|l55c|l75c|ls696|ls860|p769bk|p350|p500|p509|p870|un272|us730|vs840|vs950|ln272|ln510|ls670|ls855|lw690|mn270|mn510|p509|p769|p930|un200|un270|un510|un610|us670|us740|us760|ux265|ux840|vn271|vn530|vs660|vs700|vs740|vs750|vs910|vs920|vs930|vx9200|vx11000|ax840a|lw770|p506|p925|p999|p990|e612|d955|d802)',
        'Sony' => 'sonyst|sonylt|sonyericsson(\w+)|lt18i|lt28at|e10i|lt28h|lt26w|c5303|c6902|c6903|c6906|c6943|d2533',
        'Asus' => 'asus.*galaxy|padfone.*mobile',
        'Nokia' => '^nokia([\w\s]+)\/|lumia[\s\d]+'
	];
	protected $pad = [
		'iPad' => 'ipad|ipad.*mobile',
		'Nexus' => 'nexus[\s]+7|nexus[\s]+9|nexus[\s]+10|nexus(?:(?!mobile).)*$',
		'Samsung' => 'samsung.*tablet|galaxy.*tab|sc-01c|gt-p1000|gt-p1003|gt-p1010|gt-p3105|gt-p6210|gt-p6800|gt-p6810|gt-p7100|gt-p7300|gt-p7310|gt-p7500|gt-p7510|sch-i800|sch-i815|sch-i905|sgh-i957|sgh-i987|sgh-t849|sgh-t859|sgh-t869|sph-p100|gt-p3100|gt-p3108|gt-p3110|gt-p5100|gt-p5110|gt-p6200|gt-p7320|gt-p7511|gt-n8000|gt-p8510|sgh-i497|sph-p500|sgh-t779|sch-i705|sch-i915|gt-n8013|gt-p3113|gt-p5113|gt-p8110|gt-n8010|gt-n8005|gt-n8020|gt-p1013|gt-p6201|gt-p7501|gt-n5100|gt-n5105|gt-n5110|shv-e140k|shv-e140l|shv-e140s|shv-e150s|shv-e230k|shv-e230l|shv-e230s|shw-m180k|shw-m180l|shw-m180s|shw-m180w|shw-m300w|shw-m305w|shw-m380k|shw-m380s|shw-m380w|shw-m430w|shw-m480k|shw-m480s|shw-m480w|shw-m485w|shw-m486w|shw-m500w|gt-i9228|sch-p739|sch-i925|gt-i9200|gt-p5200|gt-p5210|gt-p5210x|sm-t311|sm-t310|sm-t310x|sm-t210|sm-t210r|sm-t211|sm-p600|sm-p601|sm-p605|sm-p900|sm-p901|sm-t217|sm-t217a|sm-t217s|sm-p6000|sm-t3100|sgh-i467|xe500|sm-t110|gt-p5220|gt-i9200x|gt-n5110x|gt-n5120|sm-p905|sm-t111|sm-t2105|sm-t315|sm-t320|sm-t320x|sm-t321|sm-t520|sm-t525|sm-t530nu|sm-t230nu|sm-t330nu|sm-t900|xe500t1c|sm-p605v|sm-p905v|sm-t337v|sm-t537v|sm-t707v|sm-t807v|sm-p600x|sm-p900x|sm-t210x|sm-t230|sm-t230x|sm-t325|gt-p7503|sm-t531|sm-t330|sm-t530|sm-t705c|sm-t535|sm-t331|sm-t800|sm-t700|sm-t537|sm-t807|sm-p907a|sm-t337a|sm-t537a|sm-t707a|sm-t807a|sm-t237|sm-t807p|sm-p607t|sm-t217t|sm-t337t|sm-t807t|sm-t116nq|sm-p550|sm-t350|sm-t550|sm-t9000|sm-p9000|sm-t705y|sm-t805',
		'Kindle' => 'kindle|silk|\b(kfot|kftt|kfjwi|kfjwa|kfote|kfsowi|kfthwi|kfthwa|kfapwi|kfapwa|wfjwae|kfsawa|kfsawi|kfaswi)\b',
		'Surface' => 'windows nt [0-9.]+; arm;.*(tablet|armbjs)',
		'BlackBerry' => 'playbook|rim tablet',
		'Philips' => '\b(pi2010|pi3000|pi3100|pi3105|pi3110|pi3205|pi3210|pi3900|pi4010|pi7000|pi7100)\b',
		'LG' => '\bl-06c|lg-v909|lg-v900|lg-v700|lg-v510|lg-v500|lg-v410|lg-v400|lg-vk810\b',
		'HP' => 'hp slate (7|8|10)|hp elitepad 900|hp-tablet|elitebook.*touch|hp 8|slate 21|hp slatebook 10',
		'Acer' => 'android.*; \b(a100|a101|a110|a200|a210|a211|a500|a501|a510|a511|a700|a701|w500|w500p|w501|w501p|w510|w511|w700|g100|g100w|b1-a71|b1-710|b1-711|a1-810|a1-811|a1-830)\b|w3-810|\ba3-a10\b|\ba3-a11\b',
		'Dell' => 'venue 11|venue 8|venue 7|dell streak 10|dell streak 7',
        'htc' => 'htc_flyer_p512|htc flyer|htc jetstream|htc-p715a|htc evo view 4g|pg41200|pg09410',
        'lenovo' => 'idea(tab|pad)( a1|a10| k1|)|thinkpad([ ]+)?tablet|lenovo.*(s2109|s2110|s5000|s6000|k3011|a3000|a3500|a1000|a2107|a2109|a1107|a5500|a7600|b6000|b8000|b8080)(-|)(fl|f|hv|h|)',
        'sony' => 'sony.*tablet|xperia tablet|sony tablet s|so-03e|sgpt12|sgpt13|sgpt114|sgpt121|sgpt122|sgpt123|sgpt111|sgpt112|sgpt113|sgpt131|sgpt132|sgpt133|sgpt211|sgpt212|sgpt213|sgp311|sgp312|sgp321|ebrd1101|ebrd1102|ebrd1201|sgp351|sgp341|sgp511|sgp512|sgp521|sgp541|sgp551|sgp621|sgp612',
        'Huawei' => 'mediapad|mediapad 7 youth|ideos s7|s7-201c|s7-202u|s7-101|s7-103|s7-104|s7-105|s7-106|s7-201|s7-slim',
        'Mipad' => '\bmi pad\b|\bhm note 1w\b'
	];

	protected $os = [
		'iOS' => '(iphone|ipod|ipad)?[\s\w]+os\s+([\w]+)\s+like',
		'Android' => 'android\s+([\w\.]+)',
		'KindleOS' => 'silk\/([\w\.]+)',
		'OSX' => 'mac[\s]+os[\s]?x\s+([\w]+)',
		'BlackBerry' => '(blackberry|\bbb10\b).*version\/([\w\.]+)|rim tablet os\s+([\w\.]+)',
        'wp' => '(windows phone|windows phone os)\s+([\w\.]+)|xblwp7|zunewp7|windows nt 6.[23]; arm;',
        'Symbian' => '(symbian|symbianos)\/([\w\.]+)|series60|series40|syb-[0-9]+|\bs60\b',
        'MeeGo' => 'meego',
        'Java' => 'j2me/|\bmidp\b|\bcldc\b',
        'webOS' => 'webos|hpwos',
	];

	protected $browser = [
        'weixin' => 'micromessenger',
		'Chrome' => 'chrome\/([\w\.]+)',
        'Opera' => 'opera.*mini|opera.*mobi|android.*opera|mobile.*opr/[0-9.]+|coast/[0-9.]+',
        'IE' => 'iemobile\/([\w\.]+)|msie\s+([\w\.]+)',
        'Firefox' => 'fennec|firefox\/([\w\.]+)|firefox.*maemo|(mobile|tablet).*firefox|firefox.*mobile',
        'Safari' => '(iphone|ipod|ipad).*version\/([\w\.]+)',
        'Tizen' => 'Tizen',
        'UC' => 'uc.*browser|ucweb',
        'baidu' => 'baiduboxapp|baidubrowser',
        'BlackBerry' => '(blackberry|\bbb10\b).*version\/([\w\.]+)|(rim tablet os).*version\/([\w\.]+)',
        'defaults' => 'android.*version\/([\w\.]+)'
	];
    
    function __construct(){
        $this->ua = strtolower($_SERVER['HTTP_USER_AGENT']);
    }


    function get_ua(){
    	return $this->ua;
    }


    function is_phone(){
    	foreach ($this->phone as $key => $val){
    		if(preg_match(sprintf('#%s#is', $val), $this->ua, $matches)){
    			return $key . '[' . $matches[count($matches) - 1] . ']';
    		}
    	}
    	return FALSE;
    }
    function is_pad(){
    	foreach ($this->pad as $key => $val){
    		if(preg_match(sprintf('#%s#is', $val), $this->ua, $matches)){
    			return $key . '[' . $matches[count($matches) - 1] . ']';
    		}
    	}
    	return FALSE;
    }

    function is_mobile(){
    	return $this->is_phone() || $this->is_pad() || preg_match('/mobile|iemobile/', $this->ua);
    }

    function is_pc(){
    	return !$this->is_mobile();
    }

    function get_os(){
    	foreach ($this->os as $key => $val){
    		if(preg_match(sprintf('#%s#is', $val), $this->ua, $matches)){
    			return $key . '[' . $matches[count($matches) - 1] . ']';
    		}
    	}
    	return 'others';
    }

    function browser(){
    	foreach ($this->browser as $key => $val){
    		if(preg_match(sprintf('#%s#is', $val), $this->ua, $matches)){
    			return $key . '[' . $matches[count($matches) - 1] . ']';
    		}
    	}
    	return 'others';
    }
}